<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog" @click.native="isShow = false">
    <!-- 表单 -->
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- disabled  readonly-->
        <el-input
          :value="form.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @click.native.stop="showDeptsTree"
        />
        <!-- 部门列表树形组件 -->
        <div v-if="isShow" class="treebox">
          <el-tree :data="treeData" :props="{label: 'name'}" @node-click="handleClick" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="form.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Enum from '@/api/constant/employees'
import { reqGetDeptsList } from '@/api/departments'
import { tranListToTreeData } from '@/utils/departments'
import { reqAddEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      hireType: Enum.hireType,
      treeData: [], // 部门列表
      isShow: false
    }
  },
  methods: {
    // 关闭对话框
    closeDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.formRef.resetFields()
    },
    async showDeptsTree() {
      this.isShow = !this.isShow // 点击一次显示 再点击一次隐藏
      if (!this.isShow) return // 隐藏不发送请求
      const { data: { depts }} = await reqGetDeptsList()
      this.treeData = tranListToTreeData(depts, '')
    },
    // 节点被点击触发的处理函数 (点击了部门)
    handleClick(data) {
      // console.log(data)
      // 禁止选中有子部门的部门
      if (data.children.length) return this.$message.warning('请选择正确的部门')
      this.form.departmentName = data.name // 设置选中的部门
      this.isShow = false
    },
    // 添加员工
    handleSubmit() {
      // 1. 预校验
      this.$refs.formRef.validate(async boo => {
        if (!boo) return
        // 2. 准备接口  携带参数发送请求
        await reqAddEmployee(this.form)
        // 3. 关闭对话框 重置表单
        this.closeDialog()
        // 4. 通知父组件重新获取一下
        this.$emit('reload')
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.treebox{
  position: absolute;
  left: 0;
  top: 45px;
  width: 50%;
  border: 1px solid #ccc;
  border-radius: 5px;
  overflow: hidden;
  z-index: 99;
}
</style>
